package com.zhang.es;

import com.zhang.util.GetEsConnClient;
import java.io.IOException;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;

/**
 * 描述:es索引操作
 *
 * @author Zhao
 * @create 2022-04-15 1:19
 */
public class EsIndex {
    private static RestHighLevelClient client = GetEsConnClient.getClient();
    public static void createIndex(String indexName){
        try {

            GetIndexRequest request = new GetIndexRequest(indexName);
            boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
            if (!exists){
                CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
                CreateIndexResponse indexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
                boolean created = indexResponse.isAcknowledged();
                if (created){
                    System.out.println("索引创建成功");
                }
            }else {
                System.out.println("索引已存在");
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
